/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-11-4
 * @Time  : 下午12:21
 * @brief :
 */

var createUi = require('common:widget/ui/base/base.js').createUi,
    Dialog = require('common:widget/ui/dialog/dialog.js').Dialog,
    IdVerify = require('common:widget/ui/idverify/idverify.js').IdVerify;

exports.Recharge = createUi({
    _data:{
        'amount':10,
        '$amount':'',
        'pid':1,
        'idVerify':false,
        'agreementTip':'请确认已阅读真融宝产品服务协议',
        'canRecharge':true,  //是否满足购买的条件
        'onBeforeRecharge':function(){}
    },

    _create:function(){
        var $root = $('.widget-recharge'),
            $bankContainer = $root.find('.bank-container'),
            $bankLimit = $root.find('.bank-limit');

        var dialog = new Dialog({
            'title':'支付提示'
        });

        this.data({
            '$root':$root,
            '$bankContainer':$bankContainer,
            '$bankLimit':$bankLimit,
            'dialog':dialog
        });

        var img = new Image();
        img.src = __uri("./image/bank.png");
    },

    showRechargeCallback:function(){
        var dialog = this.data('dialog');
        dialog.show('' +
            '<div style="text-align: left;font-size:14px;line-height: 25px;color: #515151">' +
            '请在新的窗口中登录网上银行进行付款。在付款完成后可以在我的交易记录查看充值结果，如果有问题可以稍后进行查看；' +
            '如果长时间未收到本次充值的金额，请通过网站下方联系方式联系我们' +
            '</div>');
    },


    setRechargeAmount:function(amount){
        var dialog = this.data('dialog');
        if(/^\d+$/.test(amount) && parseInt(amount)>0){
            this.data('$root').show();
            this.data('amount',amount);
            this.data("$root")
                .find('.recharge-amount').show()
                .find('span').text("￥"+amount);
            return true;
        }else{
            dialog.show("充值金额输入错误");
            this.data('$root').hide();
            this.data('amount',0);
            this.data("$root")
                .find('.recharge-amount').hide()
                .find('span').text("0");
            return false;
        }
    },

    _init:function(){
        var $root = this.data("$root"),
            $bankContainer = this.data('$bankContainer'),
            $bankLimit = this.data('$bankLimit'),
            dialog = this.data('dialog'),
            idVerify = this.data('idVerify'),
            me = this;

        $bankContainer.find(".btn-more").click(function(){
            if($bankContainer.find('.more-bank').is(":visible")){
                $bankContainer.find('.more-bank').slideUp();
            }else{
                $bankContainer.find('.more-bank').slideDown();
            }
        });

        $bankContainer.find("a").click(function(){
            if(!$(this).hasClass("btn-more")){
                $bankContainer.find("a").removeClass("active");
                $(this).addClass("active");
                $bankLimit.find('.content').hide();
                $bankLimit.find('.'+$(this).attr('data-id')+'-limit').show();
            }
        });

        $root.find(".btn-recharge").click(function(){
             me.trigger('beforeRecharge');
            var amount = me.data('$amount').val();

            if(!me.data('idVerify')){
                dialog.show('' +
                    '<div style="padding:10px;">' +
                    '<span>必须先进行实名认证才能购买，</span>' +
                    '<a href="/user/security">现在去认证</a>' +
                    '</div>' +
                    '');
                return false;
            }

            if(!!!amount || !/^\d+$/.test(amount)){
                dialog.show("充值金额必须为大于0的整数");
                return false;
            }

            if(!me.data('canRecharge')){
                dialog.show("超过了可购买的上上限");
                return false;
            }

            if(!$bankContainer.find(".active").length){
                dialog.show("请选择支付银行");
                return false;
            }

            if(!$("#agreement").is(':checked')) {
                dialog.show(me.data('agreementTip'));
                return;
            }

            var gateId = $bankContainer.find(".active").attr("data-id").substr(1);
            window.open('/pay/createpayserial?amount=' + amount + '&gate_id=' + gateId);
            this.showRechargeCallback();
            return true;

        })
    },

    hide:function(){
        this.data("$root").hide();
    }
});